package com.栈.栈基础;

import java.util.Stack;

public class LinkedListStack<E>{

    private final int capacity;
    private int size;
    private final Node<E> head = new Node<>(null, null);

    public LinkedListStack(int capacity) {
        this.capacity = capacity;
    }


    public boolean push(E value) {
        if (isFull()) {
            return false;
        }
        head.next = new Node<>(value, head.next);
        size++;
        return true;
    }


    public E pop() {
        if (isEmpty()) {
            return null;
        }
        Node<E> first = head.next;
        head.next = first.next;
        size--;
        return first.value;
    }


    public E peek() {
        if (isEmpty()) {
            return null;
        }
        return head.next.value;
    }


    public boolean isEmpty() {
        return head.next == null;
    }


    public boolean isFull() {
        return size == capacity;
    }

    static class Node<E> {
        E value;
        Node<E> next;

        public Node(E value, Node<E> next) {
            this.value = value;
            this.next = next;
        }
    }
}